package com.waterworld.watch.mine.activity;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.constraint.ConstraintLayout;
import android.util.Log;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.waterworld.watch.R;
import com.waterworld.watch.common.activity.BaseActivity;
import com.waterworld.watch.common.bean.IntimateSetBean;
import com.waterworld.watch.common.bean.UserInfoBean;
import com.waterworld.watch.common.customview.CommonPopupWindow;
import com.waterworld.watch.common.net.BaseObserverListener;
import com.waterworld.watch.common.util.TimeUtils;
import com.waterworld.watch.common.util.ScreenAdapterUtil;
import com.waterworld.watch.common.util.ToastUtils;
import com.waterworld.watch.common.util.ViewUtils;
import com.waterworld.watch.mine.interfaces.IMineManager;
import com.waterworld.watch.mine.manager.MineManager;

import butterknife.BindView;
import butterknife.ButterKnife;
import cn.carbswang.android.numberpickerview.library.NumberPickerView;

public class TimingActivity extends BaseActivity implements View.OnClickListener {

    @BindView(R.id.parentView)
    ConstraintLayout parentView;

    @BindView(R.id.header_parent)
    LinearLayout header_parent;

    @BindView(R.id.header_back)
    ImageView header_back;

    @BindView(R.id.header_title)
    TextView header_title;

    @BindView(R.id.cl_auto_boot)
    ConstraintLayout cl_auto_boot;

    @BindView(R.id.tv_auto_boot_time)
    TextView tv_auto_boot_time;

    @BindView(R.id.cb_auto_boot)
    CheckBox cb_auto_boot;

    @BindView(R.id.cl_auto_shutdown)
    ConstraintLayout cl_auto_shutdown;

    @BindView(R.id.tv_auto_shutdown_time)
    TextView tv_auto_shutdown_time;

    @BindView(R.id.cb_auto_shutdown)
    CheckBox cb_auto_shutdown;

    private CommonPopupWindow popupWindow;
    private TextView tvTitle;
    private NumberPickerView npHour;
    private NumberPickerView npMinute;
    private ConstraintLayout clCancel;
    private ConstraintLayout clConfirm;

    private IMineManager iMineManager = MineManager.getInstance();

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_me_timing);
        ButterKnife.bind(this);
        initView();
    }


    private void initView() {
        setViewSize(header_parent, ViewGroup.LayoutParams.MATCH_PARENT, ScreenAdapterUtil.getHeightPx(this) / 10);
        header_title.setText(getString(R.string.timed_shutdown));
        header_back.setVisibility(View.VISIBLE);
        header_title.setVisibility(View.VISIBLE);

        if (IntimateSetBean.getInstance().getBootTime() != null) {
            tv_auto_boot_time.setText(IntimateSetBean.getInstance().getBootTime());
        } else {
            tv_auto_boot_time.setText("00:00");
        }
        if (IntimateSetBean.getInstance().getShutdownTime() != null) {
            tv_auto_shutdown_time.setText(IntimateSetBean.getInstance().getShutdownTime());
        } else {
            tv_auto_shutdown_time.setText("00:00");
        }
        cb_auto_boot.setChecked(IntimateSetBean.getInstance().isAutoBoot());
        cb_auto_shutdown.setChecked(IntimateSetBean.getInstance().isAutoShutdown());

        if (UserInfoBean.getInstance().getWatchUser().getUserRole() == 0) {
            header_back.setOnClickListener(this);
            cl_auto_boot.setOnClickListener(this);
            cb_auto_boot.setOnClickListener(this);
            cl_auto_shutdown.setOnClickListener(this);
            cb_auto_shutdown.setOnClickListener(this);
        }
    }


    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.header_back:
                finish();
                break;
            case R.id.cl_auto_boot:
                ViewUtils.setBackgroundAlpha(this,0.5f);
                showPopupWindow(getString(R.string.open_time));
                break;
            case R.id.cb_auto_boot:
                if (cb_auto_boot.isChecked()) {
                    IntimateSetBean.getInstance().setAutoBoot(cb_auto_boot.isChecked());
                    IntimateSetBean.getInstance().setBootTime(tv_auto_boot_time.getText().toString());
                    updateIntimateSet(null, null, null, null, null, cb_auto_boot.isChecked(),
                            null, null, null, tv_auto_boot_time.getText().toString(), null);
                } else {
                    updateIntimateSet(null, null, null, null, null, cb_auto_boot.isChecked(),
                            null, null, null, null, null);
                    IntimateSetBean.getInstance().setAutoBoot(cb_auto_boot.isChecked());
                }
                break;
            case R.id.cl_auto_shutdown:
                ViewUtils.setBackgroundAlpha(this,0.5f);
                showPopupWindow(getString(R.string.close_time));
                break;
            case R.id.cb_auto_shutdown:
                if (cb_auto_shutdown.isChecked()) {
                    IntimateSetBean.getInstance().setAutoShutdown(cb_auto_shutdown.isChecked());
                    IntimateSetBean.getInstance().setShutdownTime(tv_auto_shutdown_time.getText().toString());
                    updateIntimateSet(null,null,null,null,null,null,
                            cb_auto_shutdown.isChecked(),null,null,null,tv_auto_shutdown_time.getText().toString());
                } else {
                    IntimateSetBean.getInstance().setAutoShutdown(cb_auto_shutdown.isChecked());
                    updateIntimateSet(null,null,null,null,null,null,
                            cb_auto_shutdown.isChecked(),null,null,null,null);
                }
                break;
        }
    }

    private void showPopupWindow(final String title) {
        popupWindow = new CommonPopupWindow(this, R.layout.popup_timing, (int) (ScreenAdapterUtil.getWidthPx(this) * 0.9),ViewGroup.LayoutParams.WRAP_CONTENT) {
            @Override
            protected void initView() {
                View view = getContentView();
                tvTitle = view.findViewById(R.id.tv_popup_title);
                npHour = view.findViewById(R.id.np_hour);
                npMinute = view.findViewById(R.id.np_minute);
                clCancel = view.findViewById(R.id.cl_cancel);
                clConfirm = view.findViewById(R.id.cl_confirm);
                tvTitle.setText(title);
                String[] hour = TimeUtils.returnStringArray(0,24);
                npHour.setDisplayedValues(hour);
                npHour.setMinValue(0);
                npHour.setMaxValue(hour.length - 1);
                npHour.setValue(TimeUtils.getCurrentHour24());
                String[] minute = TimeUtils.returnStringArray(0,59);
                npMinute.setDisplayedValues(minute);
                npMinute.setMinValue(0);
                npMinute.setMaxValue(minute.length - 1);
                npMinute.setValue(TimeUtils.getCurrentMinute());
            }

            @Override
            protected void initEvent() {
                clCancel.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        popupWindow.getPopupWindow().dismiss();
                    }
                });
                clConfirm.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        String data = npHour.getContentByCurrValue() + ":" +npMinute.getContentByCurrValue();
                        if (title.equals(getString(R.string.open_time))){
                            tv_auto_boot_time.setText(data);
                            cb_auto_boot.setChecked(true);
                            IntimateSetBean.getInstance().setAutoBoot(cb_auto_boot.isChecked());
                            IntimateSetBean.getInstance().setBootTime(tv_auto_boot_time.getText().toString());
                            updateIntimateSet(null,null,null,null,null,cb_auto_boot.isChecked(),
                                    null,null,null,tv_auto_boot_time.getText().toString(),null);
                        } else if (title.equals(getString(R.string.close_time))){
                            tv_auto_shutdown_time.setText(data);
                            cb_auto_shutdown.setChecked(true);
                            IntimateSetBean.getInstance().setAutoShutdown(cb_auto_shutdown.isChecked());
                            IntimateSetBean.getInstance().setShutdownTime(tv_auto_shutdown_time.getText().toString());
                            updateIntimateSet(null,null,null,null,null,null,
                                    cb_auto_shutdown.isChecked(),null,null,null,tv_auto_shutdown_time.getText().toString());
                        }
                        popupWindow.getPopupWindow().dismiss();
                    }
                });
            }
        };
        popupWindow.getPopupWindow().setOnDismissListener(new android.widget.PopupWindow.OnDismissListener() {
            @Override
            public void onDismiss() {
                ViewUtils.setBackgroundAlpha(TimingActivity.this,1.0f);
                popupWindow = null;
            }
        });
        popupWindow.showAtLocation(parentView, Gravity.CENTER, 0, 0);
    }


    private void updateIntimateSet(Boolean isReservePower, Boolean isPowerSaving, Boolean isCallPosition, Boolean isAutoAnswer, Boolean isShieldStrangeCall,
                                    Boolean isAutoBoot, Boolean isAutoShutdown, Boolean isNoShutdown, Boolean isEnableVolte, String bootTime,
                                    String shutdownTime){
        if (iMineManager != null) {
            iMineManager.updateIntimateSet(isReservePower, isPowerSaving, isCallPosition, isAutoAnswer, isShieldStrangeCall, isAutoBoot, isAutoShutdown,
                    isNoShutdown, isEnableVolte, bootTime, shutdownTime, new BaseObserverListener() {
                        @Override
                        public void onCompleted() {
                        }

                        @Override
                        public void onError(Throwable e) {
                            ToastUtils.showShort(TimingActivity.this, getString(R.string.data_error));
                            Log.e("nh",e.toString());
                        }

                        @Override
                        public void onNext(Object o) {

                        }
                    });
        }
    }
}
